package com.example.thread.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class ReentrantLockTest {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        boolean flag = false;
        try {
            log.info("尝试加锁");
            flag = lock.tryLock(5, TimeUnit.SECONDS);
            log.info("加锁成功执行业务");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            log.info("判断是否需要解锁");
            if(flag){
                lock.unlock();
                log.info("解锁成功");
            }
        }
    }
}
